package com.accesshollywood.viewFiltering;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.LinkedHashSet;

import javax.swing.DefaultListModel;

import org.jdesktop.swingx.treetable.DefaultMutableTreeTableNode;

import santhosh.DefaultMutableListModel;

import com.accesshollywood.SQL.GFXSQL;
import com.accesshollywood.SQL.IndexedSQL;
import com.accesshollywood.components.EditableListWithPopup.EditableListModel;
import com.accesshollywood.grfxOrdering.basics.Users;
import com.accesshollywood.indexables.ViewFilterPreset;
import com.accesshollywood.models.EditableOneColumnTableModel;
import com.accesshollywood.models.ViewFilterModel;

public class ViewFilters {
	public static ArrayList<ViewFilterPreset>			allViewFilterPresets;
	public static LinkedHashMap<Integer, ViewFilter>	allViewFilters;
	public static LinkedHashSet<ViewFilterGroup>		viewFilterGroups;

	public static void populateViewFilters() {
		allViewFilters = new LinkedHashMap<Integer, ViewFilter>();
		viewFilterGroups = new LinkedHashSet<ViewFilterGroup>();

		viewFilterGroups = (new ViewFiltering()).createGroups();

		for (ViewFilterGroup currGroup : viewFilterGroups) {
			for (ViewFilterTemplate currFilterTemplate : currGroup.filters) {
				ViewFilter newViewFilter = new ViewFilter();
				newViewFilter.setId(currFilterTemplate.getId());
				newViewFilter.setFilterName(currFilterTemplate.filterName);
				newViewFilter.setWhereCondition(currFilterTemplate.whereCondition);
				newViewFilter.setGroupName(currGroup.groupName);

				allViewFilters.put(newViewFilter.hashCode(), newViewFilter);
			}
		}
	}

	public static void populateViewFilterPresets() {
		allViewFilterPresets = new ArrayList<ViewFilterPreset>();
		ResultSet rawViewPresets = GFXSQL.getResultSet("viewPreset", "*", "idOwner = 0 OR idOwner = " + Users.currentUser.getId() + " ORDER BY name");
		try {
			while (rawViewPresets.next()) {
				ViewFilterPreset preset = new ViewFilterPreset();
				preset.fromSQL(rawViewPresets.getInt("idViewPreset"));
				allViewFilterPresets.add(preset);
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}
		/*
		try {
			ResultSet rawViewPresets = GFXSQL.getResultSet("viewPreset", "*", "idOwner = 0 OR idOwner = " + Users.currentUser.getId() + " ORDER BY name");
			while (rawViewPresets.next()) {
				ViewFilterPreset preset = new ViewFilterPreset();
				preset.fromSQL(rawViewPresets.getInt("idViewPreset"));

				int idViewFilterList = rawViewPresets.getInt("idViewFilterList");

				ResultSet rawViewFilterList = GFXSQL.getResultSet("ViewFilterList", "*", "idViewFilterList = " + idViewFilterList);
				while (rawViewFilterList.next()) {
					int idViewFilter = rawViewFilterList.getInt("idViewFilter");
					ViewFilter savedFilter = allViewFilters.get(idViewFilter);
					if (savedFilter != null) {
//						newPreset.checkedFilters.put(savedFilter.hashCode(), savedFilter);
					}
				}
				//allViewFilterPresets.add(newPreset);
				allViewFilterPresets.add(preset);
			}

		} catch (SQLException e) {
			e.printStackTrace();
		}
		*/
	}
/*
	public static ViewFilterModel toModel() {
		ViewFilterModel model = new ViewFilterModel();
		DefaultMutableTreeTableNode root = new DefaultMutableTreeTableNode("View Filters");
		model.setRoot(root);

		for (ViewFilterGroup currGroup : viewFilterGroups) {

			DefaultMutableTreeTableNode currGroupBranch = new DefaultMutableTreeTableNode(currGroup.groupName);
			root.add(currGroupBranch);

			// for (int i = 0; i < currGroup.filters.size(); i++) {
			for (ViewFilterTemplate currFilterTemplate : currGroup.filters) {
				ViewFilter newFilter = new ViewFilter(currFilterTemplate);
				currGroupBranch.add(newFilter);
			}
		}
		return model;
	}
*/
	public static DefaultListModel toPresetListModel() {
		DefaultListModel model = new DefaultListModel();

		for (int i = 0; i < allViewFilterPresets.size(); i++) {
			model.addElement(allViewFilterPresets.get(i));
		}

		return model;
	}

	public static EditableListModel toEditableListModel() {
		EditableListModel model = new EditableListModel();

		for (int i = 0; i < allViewFilterPresets.size(); i++) {
			model.addElement(allViewFilterPresets.get(i));
		}

		return model;
	}

	public static EditableOneColumnTableModel toTableModel() {
		EditableOneColumnTableModel model = new EditableOneColumnTableModel();

		for (int i = 0; i < allViewFilterPresets.size(); i++) {
			model.addElement(allViewFilterPresets.get(i));
		}

		return model;
	}
}
